package com.emcom.gotour.mvc.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.MessageSource;

import com.emcom.gotour.enums.BooleanEnum;
import com.emcom.gotour.enums.TourType;
import com.emcom.gotour.mvc.message.TourViewMessage;
import com.emcom.gotour.mvc.message.UpdateTourViewMessage;
import com.emcom.gotour.mvc.view.GlobalView;
import com.emcom.gotour.mvc.view.NavigationView;
import com.emcom.gotour.mvc.view.PagingView;
import com.emcom.gotour.mvc.view.SchedulerContentView;
import com.emcom.gotour.mvc.view.TourContentView;
import com.emcom.gotour.mvc.view.TourView;
import com.emcom.gotour.mvc.view.UpdateTourContentView;
import com.emcom.gotour.po.TourContent;
import com.emcom.gotour.service.TourCategoryService;
import com.emcom.gotour.vo.TourContentVO;

public class TourContentModel {
	public static String PAGING_NAME = "TOUR";
	public static TourContent getObject(HttpServletRequest request) {
		String id = request.getParameter("id");
		String typeId = request.getParameter("typeId");
		String category = request.getParameter("category");
		String subject = request.getParameter("subject");
		String aim = request.getParameter("aim");
		String characteristic = request.getParameter("characteristic");
		String location = request.getParameter("location");
		String days = request.getParameter("days");
		String cost = request.getParameter("cost");
		String costStandard = request.getParameter("costStandard");
		String costNotice = request.getParameter("costNotice");
		String attention = request.getParameter("attention");
		String statement = request.getParameter("statement");
		String comments = request.getParameter("comments");
		String isAd = request.getParameter("isAd");
		String adImageUrl = request.getParameter("adImageUrl");
		String isImage = request.getParameter("isImage");
		String imageUrl = request.getParameter("imageUrl");
		String imageUrl1 = request.getParameter("imageUrl1");
		String imageUrl2 = request.getParameter("imageUrl2");
		String imageUrl3 = request.getParameter("imageUrl3");
		String imageUrl4 = request.getParameter("imageUrl4");
		TourContent tourContent = new TourContent();
		if(StringUtils.isNotEmpty(id)) {
			tourContent.setId(Long.valueOf(id));
		}
		tourContent.setTypeId(typeId);
		tourContent.setCategory(category);
		tourContent.setSubject(subject);
		tourContent.setAim(aim);
		tourContent.setCharacteristic(characteristic);
		tourContent.setLocation(location);
		if(StringUtils.isNotEmpty(days)) {
			tourContent.setDays(Integer.valueOf(days));
		}
		if(StringUtils.isNotEmpty(cost)) {
			tourContent.setCost(new BigDecimal(cost));
		}
		tourContent.setCostStandard(costStandard);
		tourContent.setCostNotice(costNotice);
		tourContent.setAttention(attention);
		tourContent.setStatement(statement);
		tourContent.setComments(comments);
		tourContent.setIsAd(isAd);
		tourContent.setAdImageUrl(adImageUrl);
		tourContent.setIsImage(isImage);
		tourContent.setImageUrl(imageUrl);
		tourContent.setImageUrl1(imageUrl1);
		tourContent.setImageUrl2(imageUrl2);
		tourContent.setImageUrl3(imageUrl3);
		tourContent.setImageUrl4(imageUrl4);
		return tourContent;
	}

	public static List<TourContentVO> buildTourContentVOList(List<?> tourList) {
		List<TourContentVO> tourContentVOList = new ArrayList<TourContentVO>();
		for(Object tour : tourList) {
			if(tour instanceof TourContent) {
				TourContent tourContent = (TourContent)tour;
				TourContentVO contentVO = new TourContentVO();
				contentVO.setId(tourContent.getId());
				contentVO.setTypeId(tourContent.getTypeId());
				contentVO.setTypeName(TourType.valueOf(tourContent.getTypeId()).value());
				contentVO.setCategory(tourContent.getCategory());
				contentVO.setSubject(String.valueOf(tourContent.getSubject()));
				contentVO.setAim(String.valueOf(tourContent.getAim()));
				contentVO.setCharacteristic(String.valueOf(tourContent.getCharacteristic()));
				contentVO.setLocation(String.valueOf(tourContent.getLocation()));
				contentVO.setDays(tourContent.getDays());
				contentVO.setCost(tourContent.getCost());
				contentVO.setIsAd(BooleanEnum.valueOf(tourContent.getIsAd()).compareTo(BooleanEnum.TRUE) == 0 ? "广告" : "-");
				contentVO.setIsImage(BooleanEnum.valueOf(tourContent.getIsImage()).compareTo(BooleanEnum.TRUE) == 0 ? "有图片" : "-");
				tourContentVOList.add(contentVO);
			}
		}
		return tourContentVOList;
	}

	public static void setObject(HttpServletRequest request, TourContentVO tourContentVO) {
		request.setAttribute("id", tourContentVO.getId());
		request.setAttribute("typeId", tourContentVO.getTypeId());
		request.setAttribute("categoryValue", tourContentVO.getCategory());
		request.setAttribute("subject", tourContentVO.getSubject());
		request.setAttribute("aim", tourContentVO.getAim());
		request.setAttribute("characteristic", tourContentVO.getCharacteristic());
		request.setAttribute("location", tourContentVO.getLocation());
		request.setAttribute("days", tourContentVO.getDays());
		request.setAttribute("cost", tourContentVO.getCost());
		request.setAttribute("costStandard", tourContentVO.getCostStandard());
		request.setAttribute("costNotice", tourContentVO.getCostNotice());
		request.setAttribute("attention", tourContentVO.getAttention());
		request.setAttribute("statement", tourContentVO.getStatement());
		request.setAttribute("comments", tourContentVO.getComments());
		request.setAttribute("isAd", tourContentVO.getIsAd());
		request.setAttribute("adImageUrl", tourContentVO.getAdImageUrl());
		request.setAttribute("isImage", tourContentVO.getIsImage());
		request.setAttribute("imageUrl", tourContentVO.getImageUrl());
	}

	public static TourView getView(HttpServletRequest request, MessageSource messageSource, TourCategoryService tourCategoryService, List<TourContentVO> tourContentVOList) {
		TourView view = new TourView();
		view.setTitle(TourViewMessage.Title.value(request, messageSource));
		view.setCategoryList(tourCategoryService.queryCategory());
		view.setTourList(tourContentVOList);
		view.setNavigationView(new NavigationView(request, messageSource));
		view.setGlobalView(new GlobalView(request, messageSource));
		view.setTourContentView(new TourContentView(request, messageSource));
		request.setAttribute("view", view);
		request.setAttribute("pagingView", new PagingView(request, messageSource));
		return view;
	}

	public static void getUpdateView(HttpServletRequest request, MessageSource messageSource, TourCategoryService tourCategoryService, TourContentVO tourContentVO){
		UpdateTourContentView view = new UpdateTourContentView();
		view.setTitle(UpdateTourViewMessage.Title.value(request, messageSource));
		view.setCategoryList(tourCategoryService.queryCategory());
		view.setSchedulerList(tourContentVO.getTourSchedulerList());
		view.setNavigationView(new NavigationView(request, messageSource));
		view.setGlobalView(new GlobalView(request, messageSource));
		view.setTourContentView(new TourContentView(request, messageSource));
		view.setSchedulerContentView(new SchedulerContentView(request, messageSource));
		request.setAttribute("view", view);
	}

}
